﻿using System;
using System.Collections.Generic;
using WssEvents;
using WssUtils;

namespace JQMergeFramework
{
    [System.Serializable]
    public class PropItem
    {
        public int Id;
        public int Num;
        public int DayUseNum;
        public PropItem(int id,int num,int dayUseNum)
        {
            Id = id;
            Num = num;
            DayUseNum = dayUseNum;
        }
    }
    
    public class WithDrawList : SerializableDictionary<int, int> { };
    
    

    
    [System.Serializable]
    public class DataSave
    {
        //关卡等级统计
        public int lv = 0;
        

        public List<PropItem> propItems = new List<PropItem>()
        {
            //刷新道具
            new PropItem(PropId.RefreshProp,1,0),
            //清除道具
            new PropItem(PropId.RemoveProp,1,0),
            //丢弃道具
            new PropItem(PropId.DiscardProp,1,0),
            //移动道具
            new PropItem(PropId.MoveProp,1,0),
        };

        //每日复活次数
        public int dayLimitHelicopter = 2;
        //无人机使用次数
        public int dayLimitUAV = 2;
        //直升机使用次数
        public int dayLimitCopter = 2;
        
        public int currentDay = -1;

        //合成的最高等级的房子记录
        public int mergeMaxLv = -1;

        public Dictionary<int, List<int>> mergeRecordDict = new Dictionary<int, List<int>>();

        //当前已经盖的楼层
        public List<int> buildCityIdList = new List<int>();

        //当前游戏分数，结束游戏时重置
        public int currentScore = 0;

        //成就完成索引
        public int achievementFinishIndex = -1;
    }

    
    public class DataManager:Singleton<DataManager>
    {
        private DataSave mData = null;
        public DataSave Data {
            get {
                return mData;
            }
        }

        // 变更状态
        private bool mIsDirty = false;
        
        
        public void Init()
        {
            Load();
            
            if (mData == null) {
                mData = new DataSave();
            }

            DateTime NowTime = DateTime.Now.ToLocalTime();
            
      
            
            if (Data.currentDay!=NowTime.Day)
            {
                //重置每日随机事件次数
                mData.currentDay = NowTime.Day;
                mData.dayLimitHelicopter = GameCfg.DayLimitEvent;
                mData.dayLimitUAV =  GameCfg.DayLimitEvent;

                //重置每日使用次数
                for (int i = 0; i < mData.propItems.Count; i++)
                {
                    mData.propItems[i].DayUseNum = 0;
                }
                mIsDirty = true;
            }
        }
        
        
        
        public void Load(){
            SaveManager.Instance.Load<DataSave>("GAME",ref mData);
        }

        public void Save(){
            SaveManager.Instance.Save<DataSave>("GAME",mData);
        }
        
        
        
        /// <summary>
        /// 检测保存数据
        /// </summary>
        public void CheckSave()
        {
            if (mIsDirty)
            {
                mIsDirty = false;
                Save();
            }
        }


        //合成的最高等级的房子记录
        public Dictionary<int, List<int>> MergeRecord
        {
            get { return mData.mergeRecordDict; }
        }
        
        //保存合成记录
        public bool SetMergeRecordDict(UnitDataBase unitData)
        {
            if (mData.mergeRecordDict.ContainsKey(unitData.Types))
            {
                List<int> v = mData.mergeRecordDict[unitData.Types];
                if (!v.Contains(unitData.UnitLevel))
                {
                    v.Add(unitData.UnitLevel);
                    mIsDirty = true;
                    return true;
                }
            }
            else
            {
                List<int> lvList = new List<int>();
                lvList.Add(unitData.UnitLevel);
                mData.mergeRecordDict.Add(unitData.Types,lvList);
                mIsDirty = true;
                return true;
            }

            return false;
        }
        //清除合成记录
        public void MergeRecordClear()
        {
            mData.mergeRecordDict.Clear();
            mIsDirty = true;
        }

        public int Lv
        {
            get { return mData.lv; }
            set
            {
                mData.lv = value;
                mIsDirty = true;
            }
        }

        
        public int DayLimitHelicopter
        {
            get { return mData.dayLimitHelicopter; }
            set
            {
                mData.dayLimitHelicopter = value;
                mIsDirty = true;
            }
        }


        //成就完成的索引
        public int AchievementFinishIndex
        {
            get { return mData.achievementFinishIndex; }
            set
            {
                mData.achievementFinishIndex = value;
                mIsDirty = true;
            }
        }
        
        //无人机使用次数
        public int DayLimitUAV
        {
            get { return mData.dayLimitUAV; }
            set
            {
                mData.dayLimitUAV = value;
                mIsDirty = true;
            }
        }

        //直升机使用次数
        public int DayLimitCopter
        {
            get { return mData.dayLimitCopter; }
            set
            {
                mData.dayLimitCopter = value;
                mIsDirty = true;
            }
        }
        
        

        public int CurrentScore
        {
            get { return mData.currentScore; }
            set
            {
                mData.currentScore = value;
                
                //刷新目标楼层图标
                GlobalEvent.Dispatcher(GameEvents.OnRefreshScore);
                mIsDirty = true;
            }
        }

        //获取盖房子的总层数
        public List<int> BuildCityIdList
        {
            get { return mData.buildCityIdList; }
        }
        

        public void AddBuildId(int id)
        {
            mData.buildCityIdList.Add(id);
            mIsDirty = true;
        }

        public void AddBuildRange(List<int> idList)
        {
            mData.buildCityIdList.AddRange(idList);
            mIsDirty = true;
        }
        
        public void ClearBuildCityIdList()
        {
            mData.buildCityIdList.Clear();
            mIsDirty = true;
        }

        //道具列表
        public List<PropItem> PropItems
        {
            get { return mData.propItems; }
        }

        public int GetPropNum(int id)
        {
            for (int i = 0; i < mData.propItems.Count; i++)
            {
                if (id == mData.propItems[i].Id)
                {
                    return mData.propItems[i].Num;
                }
            }
            return 0;
        }

        public int GetPropDayUseNum(int id)
        {
            for (int i = 0; i < mData.propItems.Count; i++)
            {
                if (id == mData.propItems[i].Id)
                {
                    return mData.propItems[i].DayUseNum;
                }
            }
            return 0;
        }

        public void SetPropDayUseNum(int id)
        {
            for (int i = 0; i < mData.propItems.Count; i++)
            {
                if (id == mData.propItems[i].Id)
                {
                    mData.propItems[i].DayUseNum++;
                    mIsDirty = true;
                    break;
                }
            }
        }

        public void SetPropNum(int id,int num)
        {
            for (int i = 0; i < mData.propItems.Count; i++)
            {
                if (id == mData.propItems[i].Id)
                {
                    mData.propItems[i].Num += num;
                    GlobalEvent.Dispatcher(GameEvents.OnRefreshProp);
                    mIsDirty = true;
                    break;
                }
            }
        }
        

    }
}